/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */

// Sample desktop configuration
MyDesktop = new Ext.app.App({
    init :function() {
        Ext.QuickTips.init();
    },

    getModules : function() {
        return [
            new MyDesktop.ProjectSourceWindow({
                id: 'project-source',
                projectSourceControlUrl: '../project',
                windowTitle: 'Project Source Editor'
            }),
            new MyDesktop.ProjectSourceWindow({
                id: 'meta-project-source',
                projectSourceControlUrl: '../metaProject',
                windowTitle: 'Meta Project Source Editor'
            }),
            new MyDesktop.TabWindow(),
            new MyDesktop.AccordionWindow(),
            new MyDesktop.ScenariosMenuModule()
        ];
    },

    // config for the start menu
    getStartConfig : function() {
        return {
            title: 'Zbyněk Šlajchrt',
            iconCls: 'user',
            toolItems: [
                {
                    text:'Settings',
                    iconCls:'settings',
                    scope:this
                },
                '-',
                {
                    text:'Logout',
                    iconCls:'logout',
                    scope:this
                }
            ]
        };
    }
});


///*
// * Scenario window
// */
//MyDesktop.ScenariosModule = Ext.extend(Ext.app.Module, {
//    id:'scenario-win',
//    init : function() {
//        this.launcher = {
//            text: 'Scenario',
//            iconCls:'icon-grid',
//            handler : this.createWindow,
//            scope: this
//        }
//    },
//
//    createWindow : function(src) {
//        var winId = 'scenario-win-' + src.windowId;
//        var desktop = this.app.getDesktop();
//        var win = desktop.getWindow(winId);
//        if (!win) {
//
//            var propsGrid = new Ext.grid.PropertyGrid({
//
//                title: 'Navigation',
//                region:'east',
//                floatable: false,
//                margins: '5 0 0 0',
//                cmargins: '5 5 0 0',
//                padding: '0 0 0 0',
//                width: 200,
//                minSize: 100,
//                maxSize: 250,
//
//                //renderTo: 'prop-grid',
//                autoHeight: true,
//                propertyNames: {
//                    tested: 'QA',
//                    borderWidth: 'Border Width'
//                },
//                source: {},
//                viewConfig : {
//                    forceFit: true,
//                    scrollOffset: 2 // the grid will never have scroll-bars
//                },
//
//                cookiesModel: null,
//                pageName: null,
//                cachedCustomEditors: {},
//
//                onEditAllPreConfigCookies: function(message, data) {
//                    this.cookiesModel = message;
//                },
//
//                selectPage: function(pageName) {
//                    this.pageName = pageName;
//
//                    if (this.cookiesModel != null) {
//                        var pageCookies = this.cookiesModel.entries[pageName].cookies;
//                        if (pageCookies.length == 0) {
//                            propsGrid.setSource({});
//                            this.doLayout();
//                        } else {
//                            for (var i = 0; i < pageCookies.length; i++) {
//                                var pageCookie = pageCookies[i];
//                                var cookieFields = pageCookie.fields;
//
//                                var newSource = {};
//
//                                // Caching the cell editors removes the problem of not being possible to put
//                                // the cells in edit mode.
//                                var editorsForPage = this.cachedCustomEditors[pageName];
//                                var buildEditors = !editorsForPage;
//                                if (buildEditors) {
//                                    editorsForPage = {};
//                                }
//
//                                for (var j = 0; j < cookieFields.length; j++) {
//                                    var field = cookieFields[j];
//                                    newSource[field.name] = pageCookie.fieldsObject[field.name].value;
//                                    if (buildEditors) {
//                                        editorsForPage[field.name] = new Ext.grid.GridEditor(field);
//                                    }
//                                }
//
//                                this.cachedCustomEditors[pageName] = editorsForPage;
//
//                                propsGrid.setSource(newSource);
//                                propsGrid.customEditors = editorsForPage;
//                                this.doLayout();
//                            }
//                        }
//
//                    }
//                }
//
//            });
//
//            var paracol = this.app.paracol;
//
//            paracol.getAllPreConfigCookies(src.scenario, propsGrid, {
//                target: "propertySheet"
//            });
//
//            propsGrid.on('afteredit', function (e) {
//                // execute an XHR to send/commit data to the server, in callback do (if successful):
//                //alert("Edited:" + e.record.id + ":" + e.originalValue + ":" + e.value);
//
//                // update the cookie model
//                var pageCookies = this.cookiesModel.entries[this.pageName].cookies;
//                var pageCookie = pageCookies[0]; // todo: consider a variable index
//                pageCookie.fieldsObject[e.record.id].value = e.value;
//
//                // send the change to the server
//                paracol.updatePreConfigCookieProperty(this.pageName, 0, e.record.id, e.value, null);
//
//            }, propsGrid);
//
//            var saveProjectHandler = function() {
//                paracol.saveProject({
//                    onProjectSaved : function() {
//                        alert("Project saved!");
//                    }
//                })
//            };
//
//            var refresh = function(pageNameToSelect) {
//                // get the fresh pre-config cookies model from the server
//                paracol.getAllPreConfigCookies(src.scenario, {
//                    onEditAllPreConfigCookies: function(message, data) {
//                        // the fresh pre-config cookies have arrived
//                        propsGrid.onEditAllPreConfigCookies(message, data);
//
//                        // reload the scenario grid store and select the new node
//                        var scGrid = Ext.getCmp("scenario-grid");
//                        var store = scGrid.getStore();
//
//                        if (!pageNameToSelect) {
//                            var selected = scGrid.getSelectionModel().getSelected();
//                            if (selected.data.id) {
//                                pageNameToSelect = selected.data.id;
//                            }
//                        }
//
//                        store.addListener("load", function() {
//                            if (pageNameToSelect) {
//                                var newPageIndex = store.find("id", pageNameToSelect);
//                                scGrid.getSelectionModel().selectRow(newPageIndex);
//                            } else {
//                                scGrid.getSelectionModel().selectFirstRow();
//                            }
//                        }, this, {
//                            single: true // remove the listener after the event is processed
//                        });
//                        store.reload();
//
//                    }
//                }, {
//                    target: "propertySheet"
//                });
//            };
//
//            var openNewProjectEntryWindow = function() {
//                var newEntryWindowFact = new Ext.app.NewProjectEntryWindow({
//                    app: app,
//                    listeners: {
//                        entryAdded: function(ev) {
//                            refresh(ev.pageName)
//                        }
//                    }
//                });
//                newEntryWindowFact.createWindow(src.scenario, null /*metaPage filter*/);
//            };
//
//            var removeSelectedEntry = function() {
//                var scGrid = Ext.getCmp("scenario-grid");
//                var selected = scGrid.getSelectionModel().getSelected();
//                if (!selected) {
//                    alert("No node selected");
//                    return;
//                }
//
//                paracol.removeEntry(selected.data.id, {
//                    onProjectEntryRemoved: function() {
//                        var scGrid = Ext.getCmp("scenario-grid");
//                        var store = scGrid.getStore();
//                        store.addListener("load", function() {
//                            scGrid.getSelectionModel().selectFirstRow();
//                        }, this, {
//                            single: true // remove the listener after the event is processed
//                        });
//                        store.reload();
//                    }
//                })
//            };
//
//            var scenarioGrid = new Ext.grid.GridPanel({
//                id: "scenario-grid",
//                border:false,
//
//                title: 'Scenario Elements',
//                collapsible: false,
//                region: 'center',
//                margins: '5 0 0 0',
//                padding: '0 0 0 0',
//
//                ds: new Ext.data.Store({
//                    url: '../project/?scenario=' + src.scenario,
//                    reader: new Ext.data.JsonReader({
//                        root:'entries',
//                        idProperty:'id',
//                        fields: [
//                            {name: 'id', mapping: 'id'},
//                            {name: 'name', mapping: 'CommonMetaData.title'},
//                            {name: 'description', mapping: 'CommonMetaData.description'}
//                        ]
//                    }),
//                    autoLoad: true
//                }),
//
//                cm: new Ext.grid.ColumnModel([
//                    new Ext.grid.RowNumberer(),
//                    {header: "ID", width: 50, sortable: true, dataIndex: 'id'},
//                    {header: "Element Name", width: 70, sortable: true, dataIndex: 'name'},
//                    {header: "Element Description", width: 140, sortable: false, dataIndex: 'description'}
//                ]),
//
//                viewConfig: {
//                    forceFit:true
//                },
//
//                tbar:[
//                    {
//                        text:'Save Project',
//                        tooltip:'Save the project',
//                        iconCls:'add',
//                        handler: saveProjectHandler
//                    },
//                    '-',
//                    {
//                        text:'Add Node',
//                        tooltip:'Add a new node',
//                        iconCls:'add',
//                        handler: openNewProjectEntryWindow
//                    },
//                    '-',
//                    {
//                        text:'Options',
//                        tooltip:'Blah blah blah blaht',
//                        iconCls:'option'
//                    },
//                    '-',
//                    {
//                        text:'Remove Node',
//                        tooltip:'Remove the selected node',
//                        iconCls:'remove',
//                        handler: removeSelectedEntry
//                    }
//                ]
//            });
//
//            var selModel = scenarioGrid.getSelectionModel();
//            selModel.on("rowselect", function(sm, rowIndex, record) {
//                propsGrid.selectPage(record.id);
//            });
//
//            scenarioGrid.getStore().on("load", function() {
//                selModel.selectFirstRow();
//            });
//
//            scenarioGrid.on("viewready", function() {
//                selModel.selectFirstRow();
//            });
//
//            var app = this.app;
//
//            function startConfig() {
//                var pageName = selModel.getSelected().data.id;
//                var cookieWinFact = new Ext.app.CookieWindow({
//                    app: app
//                });
//                var cookieWindow = cookieWinFact.createWindow(pageName);
//                cookieWindow.on("close", function() {
//                    refresh();
//                })
//
//            }
//
//            scenarioGrid.on("keydown", function(ev) {
//                if (ev.getKey() == 13) {
//                    startConfig();
//                }
//            });
//
//            scenarioGrid.on("dblclick", function(ev) {
//                startConfig();
//            });
//
//            var border = {
//                //id:'border-panel',
//                //title: 'Border Layout',
//                layout: 'border',
//                bodyBorder: false,
//                defaults: {
//                    collapsible: true,
//                    split: true,
//                    animFloat: false,
//                    autoHide: false,
//                    useSplitTips: true,
//                    bodyStyle: 'padding:15px'
//                },
//                items: [
//                    {
//                        title: 'Footer',
//                        region: 'south',
//                        height: 150,
//                        minSize: 75,
//                        maxSize: 250,
//                        cmargins: '5 0 0 0',
//                        html: '<p>Footer content</p>'
//                    },
//                    propsGrid,
//                    scenarioGrid
//                ]
//            };
//
//            win = desktop.createWindow({
//                id: winId,
//                title: src.text + ' Scenario',
//                width:740,
//                height:480,
//                iconCls: 'icon-grid',
//                shim:false,
//                animCollapse:false,
//                constrainHeader:true,
//
//                layout: 'fit',
//                items: border
//            });
//        }
//        win.show();
//    }
//});

//MyDesktop.GridWindow = Ext.extend(Ext.app.Module, {
//    id:'project-source',
//    init : function() {
//        this.launcher = {
//            text: 'Project Source Editor',
//            iconCls:'icon-grid',
//            handler : this.createWindow,
//            scope: this
//        }
//    },
//
//    createWindow : function() {
//        var paracol = this.app.paracol;
//        var desktop = this.app.getDesktop();
//        var win = desktop.getWindow('project-source-win');
//        if (!win) {
//
//            function loadText() {
//                paracol.postCustomMessage("GET", "../project/dump", {
//                    onResponse: function(response) {
//                        Ext.getCmp('project-source-text-area').setValue(response);
//                    }
//                })
//            }
//
//            function storeText() {
//                var storeIt = confirm("Really store the project source?");
//                if (storeIt) {
//                    var projectSource = Ext.getCmp('project-source-text-area').getValue();
//                    paracol.postCustomMessage("POST", "../project/store", {
//                        onResponse: function(response) {
//                            alert("Project source stored");
//                        }
//                    }, projectSource)
//                }
//            }
//
//            win = desktop.createWindow({
//                id: 'project-source-win',
//                title:'Project Source Editor',
//                width:740,
//                height:480,
//                iconCls: 'icon-grid',
//                shim:false,
//                animCollapse:false,
//                constrainHeader:true,
//
//                layout: 'fit',
//                items:
//                        new Ext.Panel({
//                            layout:'anchor',
//                            anchorSize: {width:800, height:600},
//                            border:false,
//
//                            viewConfig: {
//                                forceFit:true
//                            },
//                            //autoExpandColumn:'company',
//
//                            items: [
//                                {
//                                    xtype: 'textarea',
//                                    id: 'project-source-text-area',
//                                    name: 'description',
//                                    anchor: '100% 100%',
//                                    hideLabel: true
//                                }
//                            ],
//
//                            tbar:[
//                                {
//                                    text:'Save Project',
//                                    tooltip:'Save Project',
//                                    iconCls:'add',
//                                    handler: storeText
//
//                                },
//                                '-',
//                                {
//                                    text:'Refresh',
//                                    tooltip:'Refresh',
//                                    iconCls:'option',
//                                    handler: loadText
//                                }
//                            ]
//                        })
//            });
//
//            loadText();
//
//        }
//        win.show();
//    }
//});


//////////////////////////////

// todo: remove what is below

MyDesktop.TabWindow = Ext.extend(Ext.app.Module, {
    id:'tab-win',
    init : function() {
        this.launcher = {
            text: 'Tab Window',
            iconCls:'tabs',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('tab-win');
        if (!win) {
            win = desktop.createWindow({
                id: 'tab-win',
                title:'Tab Window',
                width:740,
                height:480,
                iconCls: 'tabs',
                shim:false,
                animCollapse:false,
                border:false,
                constrainHeader:true,

                layout: 'fit',
                items:
                        new Ext.TabPanel({
                            activeTab:0,

                            items: [
                                {
                                    title: 'Tab Text 1',
                                    header:false,
                                    html : '<p>Something useful would be in here.</p>',
                                    border:false
                                },
                                {
                                    title: 'Tab Text 2',
                                    header:false,
                                    html : '<p>Something useful would be in here.</p>',
                                    border:false
                                },
                                {
                                    title: 'Tab Text 3',
                                    header:false,
                                    html : '<p>Something useful would be in here.</p>',
                                    border:false
                                },
                                {
                                    title: 'Tab Text 4',
                                    header:false,
                                    html : '<p>Something useful would be in here.</p>',
                                    border:false
                                }
                            ]
                        })
            });
        }
        win.show();
    }
});


MyDesktop.AccordionWindow = Ext.extend(Ext.app.Module, {
    id:'acc-win',
    init : function() {
        this.launcher = {
            text: 'Accordion Window',
            iconCls:'accordion',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('acc-win');
        if (!win) {
            win = desktop.createWindow({
                id: 'acc-win',
                title: 'Accordion Window',
                width:250,
                height:400,
                iconCls: 'accordion',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                tbar:[
                    {
                        tooltip:{title:'Rich Tooltips', text:'Let your users know what they can do!'},
                        iconCls:'connect'
                    },
                    '-',
                    {
                        tooltip:'Add a new user',
                        iconCls:'user-add'
                    },
                    ' ',
                    {
                        tooltip:'Remove the selected user',
                        iconCls:'user-delete'
                    }
                ],

                layout:'accordion',
                border:false,
                layoutConfig: {
                    animate:false
                },

                items: [
                    new Ext.tree.TreePanel({
                        id:'im-tree',
                        title: 'Online Users',
                        loader: new Ext.tree.TreeLoader(),
                        rootVisible:false,
                        lines:false,
                        autoScroll:true,
                        tools:[
                            {
                                id:'refresh',
                                on:{
                                    click: function() {
                                        var tree = Ext.getCmp('im-tree');
                                        tree.body.mask('Loading', 'x-mask-loading');
                                        tree.root.reload();
                                        tree.root.collapse(true, false);
                                        setTimeout(function() { // mimic a server call
                                            tree.body.unmask();
                                            tree.root.expand(true, true);
                                        }, 1000);
                                    }
                                }
                            }
                        ],
                        root: new Ext.tree.AsyncTreeNode({
                            text:'Online',
                            children:[
                                {
                                    text:'Friends',
                                    expanded:true,
                                    children:[
                                        {
                                            text:'Jack',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Brian',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Jon',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Tim',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Nige',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Fred',
                                            iconCls:'user',
                                            leaf:true
                                        },
                                        {
                                            text:'Bob',
                                            iconCls:'user',
                                            leaf:true
                                        }
                                    ]
                                },
                                {
                                    text:'Family',
                                    expanded:true,
                                    children:[
                                        {
                                            text:'Kelly',
                                            iconCls:'user-girl',
                                            leaf:true
                                        },
                                        {
                                            text:'Sara',
                                            iconCls:'user-girl',
                                            leaf:true
                                        },
                                        {
                                            text:'Zack',
                                            iconCls:'user-kid',
                                            leaf:true
                                        },
                                        {
                                            text:'John',
                                            iconCls:'user-kid',
                                            leaf:true
                                        }
                                    ]
                                }
                            ]
                        })
                    }), {
                        title: 'Settings',
                        html:'<p>Something useful would be in here.</p>',
                        autoScroll:true
                    },{
                        title: 'Even More Stuff',
                        html : '<p>Something useful would be in here.</p>'
                    },{
                        title: 'My Stuff',
                        html : '<p>Something useful would be in here.</p>'
                    }
                ]
            });
        }
        win.show();
    }
});

// for example purposes
var windowIndex = 0;

MyDesktop.BogusModule = Ext.extend(Ext.app.Module, {
    init : function() {
        this.launcher = {
            text: 'Window ' + (++windowIndex),
            iconCls:'bogus',
            handler : this.createWindow,
            scope: this,
            windowId:windowIndex
        }
    },

    createWindow : function(src) {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('bogus' + src.windowId);
        if (!win) {
            win = desktop.createWindow({
                id: 'bogus' + src.windowId,
                title:src.text,
                width:640,
                height:480,
                html : '<p>Something useful would be in here.</p>',
                iconCls: 'bogus',
                shim:false,
                animCollapse:false,
                constrainHeader:true
            });
        }
        win.show();
    }
});


// Array data for the grid
Ext.grid.dummyData = [
    ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
    ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
    ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
    ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
    ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
    ['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],
    ['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'],
    ['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'],
    ['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],
    ['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'],
    ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
    ['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],
    ['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],
    ['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],
    ['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],
    ['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'],
    ['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'],
    ['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'],
    ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am'],
    ['Walt Disney Company (The) (Holding Company)',29.89,0.24,0.81,'9/1 12:00am']
];